/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.util.Collections;
/*     */ import java.util.Date;
/*     */ import org.apache.lucene.document.DateTools;
/*     */ import org.apache.lucene.document.DateTools.Resolution;
/*     */ import org.apache.lucene.index.CorruptIndexException;
/*     */ import org.apache.lucene.index.SegmentInfos;
/*     */ import org.apache.lucene.store.ChecksumIndexInput;
/*     */ import org.apache.lucene.store.ChecksumIndexOutput;
/*     */ import org.apache.lucene.store.Directory;
/*     */ import org.apache.lucene.store.FSDirectory;
/*     */ import org.apache.lucene.store.IndexOutput;
/*     */ import org.apache.lucene.util.BitVector;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class LuceneIndexRepair
/*     */ {
/*     */   private static final int FORMAT = -11;
/*     */   private static final String SEGMENTS_GEN = "segments.gen";
/*     */   private static final String SEGMENTS = "segments";
/*     */   private static final String DELETES_EXTENSION = "del";
/*     */   private final Directory directory;
/*  57 */   private int repaired = 0;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   LuceneIndexRepair(Directory dir)
/*     */   {
/*  65 */     this.directory = dir;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   int repair()
/*     */     throws IOException
/*     */   {
/*  75 */     String segsFilename = SegmentInfos.getCurrentSegmentFileName(this.directory);
/*  76 */     long gen = SegmentInfos.generationFromSegmentsFileName(segsFilename);
/*  77 */     String nextSegsFilename = getSegmentsFilename(++gen);
/*     */     
/*  79 */     ChecksumIndexInput input = new ChecksumIndexInput(this.directory.openInput(segsFilename));
/*     */     try
/*     */     {
/*  82 */       ChecksumIndexOutput output = new ChecksumIndexOutput(this.directory.createOutput(nextSegsFilename));
/*     */       try
/*     */       {
/*  85 */         convert(input, output);
/*     */       }
/*     */       finally {}
/*     */     }
/*     */     finally {
/*  90 */       input.close();
/*     */     }
/*     */     
/*  93 */     if (this.repaired == 0) {
/*  94 */       this.directory.deleteFile(nextSegsFilename);
/*  95 */       return this.repaired;
/*     */     }
/*     */     
/*  98 */     this.directory.sync(Collections.singleton(nextSegsFilename));
/*     */     try {
/* 100 */       commit(gen);
/*     */     } catch (IOException e) {
/* 102 */       this.directory.deleteFile(nextSegsFilename);
/* 103 */       throw e;
/*     */     }
/*     */     
/* 106 */     String backupFilename = "REPAIR_" + DateTools.dateToString(new Date(), DateTools.Resolution.SECOND) + "." + segsFilename;
/*     */     
/*     */ 
/* 109 */     rename(segsFilename, backupFilename);
/* 110 */     return this.repaired;
/*     */   }
/*     */   
/*     */   private void commit(long gen) throws IOException {
/* 114 */     IndexOutput output = this.directory.createOutput("segments.gen");
/*     */     try {
/* 116 */       output.writeInt(-2);
/* 117 */       output.writeLong(gen);
/* 118 */       output.writeLong(gen);
/*     */     } finally {
/* 120 */       output.close();
/*     */     }
/* 122 */     this.directory.sync(Collections.singleton("segments.gen"));
/*     */   }
/*     */   
/*     */   private void convert(ChecksumIndexInput input, ChecksumIndexOutput output) throws IOException {
/* 126 */     int format = input.readInt();
/* 127 */     if (format < 0) {
/* 128 */       if (format < -11) {
/* 129 */         throw new CorruptIndexException("Unknown format version: " + format);
/*     */       }
/* 131 */       output.writeInt(format);
/* 132 */       long version = input.readLong();
/* 133 */       version += 1L;
/* 134 */       output.writeLong(version);
/* 135 */       output.writeInt(input.readInt());
/*     */     }
/*     */     else {
/* 138 */       output.writeInt(format);
/*     */     }
/*     */     
/* 141 */     int num = input.readInt();
/* 142 */     output.writeInt(num);
/* 143 */     for (int i = 0; i < num; i++) {
/* 144 */       if (format <= -11) {
/* 145 */         output.writeString(input.readString());
/*     */       }
/* 147 */       String name = input.readString();
/* 148 */       output.writeString(name);
/* 149 */       int count = input.readInt();
/* 150 */       output.writeInt(count);
/* 151 */       long delGen = -1L;
/* 152 */       if (format <= -2) {
/* 153 */         delGen = input.readLong();
/* 154 */         output.writeLong(delGen);
/* 155 */         if (format <= -4) {
/* 156 */           int docStoreOffset = input.readInt();
/* 157 */           output.writeInt(docStoreOffset);
/* 158 */           if (docStoreOffset != -1) {
/* 159 */             output.writeString(input.readString());
/* 160 */             output.writeByte(input.readByte());
/*     */           }
/*     */         }
/*     */       }
/* 164 */       if (format <= -3) {
/* 165 */         output.writeByte(input.readByte());
/*     */       }
/* 167 */       int numNormGen = input.readInt();
/* 168 */       output.writeInt(numNormGen);
/* 169 */       if (numNormGen > 0) {
/* 170 */         for (int j = 0; j < numNormGen; j++) {
/* 171 */           output.writeLong(input.readLong());
/*     */         }
/*     */       }
/* 174 */       output.writeByte(input.readByte());
/* 175 */       if (format <= -6) {
/* 176 */         int delCount = input.readInt();
/* 177 */         if ((delCount <= count) && (delCount == getDelCount(name, delGen))) {
/* 178 */           output.writeInt(delCount);
/*     */         }
/*     */         else {
/* 181 */           this.repaired += 1;
/* 182 */           output.writeInt(-1);
/*     */         }
/*     */       }
/* 185 */       if (format <= -7) {
/* 186 */         output.writeByte(input.readByte());
/*     */       }
/* 188 */       if (format <= -9) {
/* 189 */         output.writeStringStringMap(input.readStringStringMap());
/*     */       }
/* 191 */       if (format <= -10) {
/* 192 */         output.writeByte(input.readByte());
/*     */       }
/*     */     }
/*     */     
/* 196 */     if ((format >= 0) && 
/* 197 */       (input.getFilePointer() < input.length())) {
/* 198 */       output.writeLong(input.readLong());
/*     */     }
/*     */     
/*     */ 
/* 202 */     if (format <= -8) {
/* 203 */       if (format <= -9) {
/* 204 */         output.writeStringStringMap(input.readStringStringMap());
/*     */       } else {
/* 206 */         output.writeByte(input.readByte());
/* 207 */         output.writeString(input.readString());
/*     */       }
/*     */     }
/*     */     
/* 211 */     if (format <= -5) {
/* 212 */       long checksumNow = input.getChecksum();
/* 213 */       long checksumThen = input.readLong();
/* 214 */       if (checksumNow != checksumThen) {
/* 215 */         throw new CorruptIndexException("checksum mismatch in segments file");
/*     */       }
/*     */     }
/*     */     
/* 219 */     output.finishCommit();
/*     */   }
/*     */   
/*     */   private int getDelCount(String name, long gen) throws IOException {
/* 223 */     if (gen < 0L)
/* 224 */       return 0;
/* 225 */     if (gen == 0L) {
/* 226 */       String filename = name + "." + "del";
/* 227 */       if (this.directory.fileExists(filename)) {
/* 228 */         BitVector del = new BitVector(this.directory, filename);
/* 229 */         return del.count();
/*     */       }
/* 231 */       return 0;
/*     */     }
/*     */     
/* 234 */     String filename = name + "_" + Long.toString(gen, 36) + "." + "del";
/*     */     
/* 236 */     if (this.directory.fileExists(filename)) {
/* 237 */       BitVector del = new BitVector(this.directory, filename);
/* 238 */       return del.count();
/*     */     }
/* 240 */     return 0;
/*     */   }
/*     */   
/*     */ 
/*     */   private String getSegmentsFilename(long gen)
/*     */   {
/* 246 */     return "segments_" + Long.toString(gen, 36);
/*     */   }
/*     */   
/*     */   private void rename(String from, String to) {
/*     */     File dir;
/* 251 */     if ((this.directory instanceof LuceneDirectory)) {
/* 252 */       dir = ((LuceneDirectory)this.directory).getDirectory(); } else { File dir;
/* 253 */       if ((this.directory instanceof FSDirectory))
/* 254 */         dir = ((FSDirectory)this.directory).getDirectory(); else
/*     */         return;
/*     */     }
/*     */     File dir;
/* 258 */     File renameFrom = new File(dir, from);
/* 259 */     File renameTo = new File(dir, to);
/* 260 */     if (renameTo.exists()) {
/* 261 */       renameTo.delete();
/*     */     }
/* 263 */     renameFrom.renameTo(renameTo);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/LuceneIndexRepair.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */